package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.data.cache.db.DataBaseManager;
import com.zw.yzk.kabu.data.cache.db.User;
import com.zw.yzk.kabu.internal.component.DaggerModifyUserInfoComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.ModifyUserInfoModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.DistrictModel;
import com.zw.yzk.kabu.presenter.ModifyUserInfoPresenter;
import com.zw.yzk.kabu.utils.CityUtils;
import com.zw.yzk.kabu.view.view.ModifyUserInfoView;
import com.zw.yzk.kabu.view.widget.AlertDialogUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2017/2/11.
 */

public class ModifyUserInfoActivity extends BaseActivity implements ModifyUserInfoView {

    @Bind(R.id.modify_nick_name)
    TextView nickName;
    @Bind(R.id.modify_sex)
    TextView sex;
    @Bind(R.id.modify_region)
    TextView region;
    @Bind(R.id.modify_introduction)
    TextView introduction;

    private List<String> provinceList;
    private List<List<String>> cityList;

    private String province;
    private String city;
    @Inject
    ModifyUserInfoPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_user_info;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.modify_user_info))
                .build();
    }

    @Override
    public void initComponent() {
        DaggerModifyUserInfoComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .modifyUserInfoModule(new ModifyUserInfoModule())
                .build()
                .inject(this);

        presenter.setView(this);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initPage();
        loadDistrictData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @OnClick(R.id.layout_modify_nick_name)
    public void modifyNickName() {
        startActivityForResult(new Intent(this, EditNickNameActivity.class), ActivityConstant.EDIT_NICKNAME_CODE);
    }

    @OnClick(R.id.layout_modify_sex)
    public void modifySex() {
        AlertDialogUtil.showSelectSexDialog(this, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sex.setText(R.string.sex_male);
                modifyUserInfo(nickName.getText().toString(), getSex(), introduction.getText().toString());
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sex.setText(R.string.sex_female);
                modifyUserInfo(nickName.getText().toString(), getSex(), introduction.getText().toString());
            }
        }).show();
    }

    @OnClick(R.id.layout_modify_region)
    public void modifyRegion() {
        showSelectDistrictDialog();
    }

    @OnClick(R.id.layout_modify_introduction)
    public void modifyIntroduction() {
        startActivityForResult(new Intent(this, EditIntroductionActivity.class), ActivityConstant.EDIT_INTRODUCTION_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK || data == null) {
            return;
        }
        switch (requestCode) {
            case ActivityConstant.EDIT_INTRODUCTION_CODE:
                introduction.setText(data.getStringExtra(ActivityConstant.EDIT_INTRODUCTION));
                modifyUserInfo(null, getSex(), introduction.getText().toString());
                break;
            case ActivityConstant.EDIT_NICKNAME_CODE:
                nickName.setText(data.getStringExtra(ActivityConstant.EDIT_NICKNAME));
                modifyUserInfo(nickName.getText().toString(), getSex(), null);
                break;
            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 初始化页面显示
     */
    private void initPage() {
        User user = DataBaseManager.getInstance().getLoginUser();
        if (user == null) {
            return;
        }
        nickName.setText(TextUtils.isEmpty(user.getNickName()) ? "" : user.getNickName());
        if (user.getSex() == 0) {
            sex.setText(getResources().getString(R.string.sex_female));
        } else if (user.getSex() == 1) {
            sex.setText(getResources().getString(R.string.sex_male));
        }
        setRegion(user.getProvince(), user.getCity());
        introduction.setText(user.getIntroduction());
    }

    /**
     * 获取性别
     *
     * @return 0:女，1;男，2：未知
     */
    private int getSex() {
        String sexStr = sex.getText().toString();
        if (sexStr.equals(getResources().getString(R.string.sex_male))) {
            return 1;
        } else if (sexStr.equals(getResources().getString(R.string.sex_female))) {
            return 0;
        } else {
            return 2;
        }
    }

    /**
     * 加载地区信息
     */
    private void loadDistrictData() {
        provinceList = new ArrayList<>();
        cityList = new ArrayList<>();
        DistrictModel[] data = CityUtils.getLocalCityList(this);
        if (data == null) {
            return;
        }
        for (DistrictModel model : data) {
            provinceList.add(model.district);
            cityList.add(model.children);
        }
    }

    /**
     * 设置地区信息
     *
     * @param p 省
     * @param c 城市
     */
    private void setRegion(String p, String c) {
        province = p;
        city = c;
        region.setText(TextUtils.isEmpty(province) ? "" : province + (TextUtils.isEmpty(city) ? "" : city));
    }

    /**
     * 修改个人信息
     *
     * @param nickName     昵称
     * @param sex          性别
     * @param introduction 签名
     */
    private void modifyUserInfo(String nickName, int sex, String introduction) {
        presenter.modifyUserInfo(nickName, sex, province, city, introduction);
    }

    /**
     * 显示地区选择对话框
     */
    private void showSelectDistrictDialog() {
        if (provinceList.isEmpty()) {
            return;
        }
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                setRegion(provinceList.get(options1), cityList.get(options1).get(options2));
                modifyUserInfo(nickName.getText().toString(), getSex(), introduction.getText().toString());
            }
        }).build();
        pvOptions.setPicker(provinceList, cityList);
        pvOptions.show();
    }
}
